# -- coding:utf-8 --
#!/usr/bin/python
import sys, os
sys.path.append("/home/avatar/Minibot/04.RobotSamples/01.Arm")
import inspect
from LoadJson import LoadJson
from BwSensorLib import BwSensorLib
from ArmRobot import ArmRobot
from BwRobotLib import BwRobotLib
from MiniBotControlLib import MiniBotControlLib
import time
import BwHelpLib 
import random

# 加载机器人构型配置
fileName = inspect.getframeinfo(inspect.currentframe()).filename
folderPath = os.path.dirname(os.path.abspath(fileName))
config_file = folderPath+"/config.txt"

	
class RandomGrab:			
	def __init__(self):
		# 实例化MiniBotControlLib类，并设置为udp模式
		self.mlib = MiniBotControlLib("udp")
		# 输入ip地址和端口号
		self.ip = "10.10.100.254"
		self.port = 8899
		# 连接机器人,并设置超时时间5s
		self.mlib.Connect(self.ip, self.port, 2000)
		# 实例化BwRobotLib类，并将mlib作为参数传入
		self.robotlib = BwRobotLib(self.mlib)
		# 导入配置文件，获取文件中的模块ID和模块连接位置
		self.module_ids, self.module_ids_loc = self.robotlib.loadConfig(config_file)
		# 扫描当前机器人构型的ID，默认扫描ID段为20-50
		self.module_ids_scan, self.module_ids_loc_scan = self.mlib.ScanId()
		# 判断是否扫描的ID与配置文件导入的ID一致
		res = BwHelpLib.checkListEqual(self.module_ids,self.module_ids_scan)
		if res == False:
			print("\033[31m输入的模块ID与扫描获取的模块ID不一致，请检查！\033[37m")
			print("配置文件中模块ID: ", self.module_ids)
			print("实际扫描的模块ID: ", self.module_ids_scan)
			exit(0)
		else:
			print("输入的模块ID与扫描获取的模块ID一致")
			
		# 实例化操作臂类
		self.armlib = ArmRobot(self.mlib)
		# 将模块ID与操作臂构型绑定
		self.armlib.bindIds(self.module_ids)		
		# 实例化传感器库对象
		self.sensorlib = BwSensorLib(self.mlib)
		# 动作延时时间
		self.wait_time = 1.5
		# 动作速度
		self.speed = 50	
		# 点阵显示图案 
		# 图案分别为：笑脸　惊喜　无语
		self.display_data = [[0x36,0x49,0x49,0x00,0x3E],[0x22,0x77,0x22,0x1C,0x14],[0x41,0x36,0x00,0x1C,0x22]]

		# 动作组数据
		self.GripHang = []
		self.GripRed = []
		self.GripGreen = []
		self.GripBlue = []

		# 实例化导入动作组数据对象
		self.load_json = LoadJson()
		# 机械臂从A-C点来回摇晃的运动序列
		self.GripHang = self.load_json.LoadActionDataHang(self.GripHang)
		# 抓夹红色点的运动序列
		self.GripRed = self.load_json.LoadActionDataGripRed(self.GripRed)
		# 抓夹绿色点的运动序列
		self.GripGreen = self.load_json.LoadActionDataGripGreen(self.GripGreen)
		# 抓夹蓝色点的运动序列
		self.GripBlue = self.load_json.LoadActionDataGripBlue(self.GripBlue)
		# 初始化动作	
		self.initRobotMove()		

	# 初始化至实验开始位置
	def initRobotMove(self, _time=2):
		angs = self.GripHang[0]
		speed = 20
		self.armlib.position_angles = angs
		self.armlib.pushJointsAngles(speed, self.wait_time)
		time.sleep(_time)		

# ####随机抓取
	def randomGrab(self):
		print("请按下'1'进行抽奖")
		for a in range(1,4): 
			self.armlib.position_angles = self.GripHang[a]
			self.armlib.pushJointsAngles(self.speed,self.wait_time)
		s1 = self.sensorlib.readRemoteSensor()
		if(s1==10):
			# 生成随机数
			s2=random.randint(0,2)
			if(s2==0):
				print("抓'红'")
				self.sensorlib.setMatrixSensor(self.display_data[0]) # 点阵显示笑脸表情
				for a in range(0,9): # 从初始位置到抓夹红色点方块有9组动作
					self.armlib.position_angles = self.GripRed[a]
					self.armlib.pushJointsAngles(self.speed,self.wait_time)
					if(a==3):# 抓起来定住
						time.sleep(self.wait_time)
			elif(s2==1):
				print("抓'绿'")
				self.sensorlib.setMatrixSensor(self.display_data[1]) # 点阵显示惊喜表情
				for a in range(0,11): # 从初始位置到抓夹绿色点方块有11组动作
					self.armlib.position_angles = self.GripGreen[a]
					self.armlib.pushJointsAngles(self.speed,self.wait_time)
					if(a==3):# 抓起来定住
						time.sleep(self.wait_time)
			else:
				print("抓'蓝'")
				self.sensorlib.setMatrixSensor(self.display_data[2]) # 点阵显示无语表情
				for a in range(0,10): # 从初始位置到抓夹蓝色点方块有10组动作
					self.armlib.position_angles = self.GripBlue[a]
					self.armlib.pushJointsAngles(self.speed,self.wait_time)
					if(a==3):# 抓起来定住
						time.sleep(self.wait_time)	
				

# 主函数
def main():
	# 实例化随机抽奖类
	randomgrab = RandomGrab()
	# 随机抽奖功能3次
	for i in range(3):
		randomgrab.randomGrab()


if __name__ == "__main__":
	try:
		main()
	except KeyboardInterrupt:
		print(" Program closed! ")
	pass